<?php

ini_set('include_path', '../');

require_once("lib/exception/duplicate_instance_exception.php");
require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");

require_once("util/configuration_parametters.php");

require_once("model/article_comment/to/article_comment_to.php");
require_once("model/article_comment/dao/article_comment_dao_factory.php");

require_once("model/article/to/article_to.php");
require_once("model/article/dao/article_dao_factory.php");

require_once("model/autor/to/autor_to.php");
require_once("model/autor/dao/autor_dao_factory.php");


class Article_comment_dao_test {

	private $dao;
	private $autor;
	private $article;
	
	public function __construct() {}
	
	public function before_test() {		
		try {
			$this->autor = (object) Autor_dao_factory::singleton()->get_dao()->create(new Autor_to('xxxxxxxxxxxx','password','email'));		
			$date = date("Y-m-d H:i:s");
			$this->article = (object) Article_dao_factory::singleton()->get_dao()->create(new Article_to(0, 'title', $this->autor->get_login(), $date, 'open', 0));
			
			$this->dao = Article_comment_dao_factory::singleton()->get_dao();
		}
		catch (Exception $e) {
			$e->getMessage() . '</br>';
		}
	}
	
	public function run_test() {
		try {
			$date = date("Y-m-d H:i:s");
			$article_comment_to = new Article_comment_to(0, 'title', 'content', $this->article->get_number(), 
									$this->autor->get_login(), $date, 'open');
			
			//create
			$article_comment_to = $this->dao->create($article_comment_to);	
			echo 'create article_comment: ' . $article_comment_to->to_string() . '<br/>';
			
			//create existent 
			try {
				$this->dao->create($article_comment_to);
				throw new Exception('create duplicate');
			}
			catch (Duplicate_instance_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//exist inexistent 
			$this->dao->exist(9999);
			echo 'No exist article_comment 9999<br/>';
			
			//exist existent 
			$this->dao->exist($article_comment_to->get_number());
			echo 'exist article_comment: ' . $article_comment_to->to_string() . '<br/>';

			//find inexistent 
			try {
				$this->dao->find_by_comment_number(9999);
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//find existent 
			$aux_article_comment_to = $this->dao->find_by_comment_number($article_comment_to->get_number());	
				if ($article_comment_to->equals($aux_article_comment_to)) 
					echo 'found article_comment: ' . $article_comment_to->to_string() . '<br/>';
				else
					echo 'not equal article_comment inserted';
			
			//find comments by article inexistent	
			try {				
				$list_comments = $this->dao->find_by_article_number(9999);	
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
					
			//find comments by article existen
			$list_comments = $this->dao->find_by_article_number($this->article->get_number());	
				echo 'Find by article: </br>';
				foreach($list_comments as $comment)
					echo 'Comment: ' . $comment->to_string() . '</br>';
			
			//count comments by autor
			$comments = $this->dao->count_comments_by_login($this->autor->get_login());
			echo 'Count comments by autor: ' . $comments . '</br>';	
					
			//update 
			$article_comment_to->set_state('wait');
			$this->dao->update($article_comment_to);
			$aux_article_ping_to = $this->dao->find_by_comment_number($article_comment_to->get_number());	
			echo 'update article_comment: ' . $aux_article_ping_to->to_string() . '<br/>';

			//remove inexistent 	
			try {
				$this->dao->remove(9999);
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//remove existent 
			$this->dao->remove($article_comment_to->get_number());
			echo 'remove article_comment: ' . $article_comment_to->to_string() . '<br/>';
				
		}
		catch (Duplicate_instance_exception $e) {
			echo $e->getMessage();
		}
		catch (Internal_error_exception $e) {
			echo $e->getMessage();
		}
		catch (Instance_not_found_exception $e) {
			echo $e->getMessage();
		}
		catch (Exception $e) {
			echo $e->getMessage();
		}
	}
	
	public function after_test() {
		Article_dao_factory::singleton()->get_dao()->remove($this->article->get_number());
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor->get_login());
	}
}

//test
$test = new Article_comment_dao_test();

$test->before_test();
$test->run_test();
$test->after_test();

?>